<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="./css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="./css/header.css" />
    <script src="./js/jquery.js"></script>
    <script src="./js/bootstrap.js"></script>
    <title>Document</title>
    <style>
      #mainer {
        min-height: calc(100vh - 170px);
        overflow: hidden;
      }
      #news {
        padding: 0;
        margin: 0;
        list-style-type: none;
      }
      #news-box {
        background-color:white;
        border: 1px solid black;
        height: 260px;
        overflow: hidden;
      }
      #news li {
        background-color: white;
        list-style: none;
        margin-bottom: 10px;
        height: 80px;
        border: 1px solid green;
        overflow: hidden;
      }
      #news li img {
        width: 100%;
        height: 80px;
      }
      .news-item-img,
      .news-item-box {
        float: left;
      }
      .news-item-img{
        width:30%;
      }
      .news-item-box {
        padding-left: 10px;
        width:70%;
      }
      .news-item-box-title {
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        font-size: 15px;
        padding-top: 10px;
      }
      .news-item-content {
        overflow: hidden;
      }
      .news-item-box-title a {
        color: #00bfff;
        font-size: 15px;
      }
      .news-item-box-title a:hover {
        text-decoration: none;
      }
      * {
        box-sizing: border-box;
      }
      #box {
        height: 200px;

        overflow: hidden;
        position: relative;
      }
      .father {
        height: 200px;
        width: 1250px;
        position: absolute;
        top: 0;
        left: 0;
      }
      .son {
        width: 200px;
        height: 180px;
        float: left;
        padding-left: 15px;
      }
      .son a {
        display: block;
        width: 100%;
        height: 100%;
      }
      .son img {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="bg">
      <div id="header"></div>
      <div id="mainer" class="container">
        <div class="page-header" style="margin-top: 0; color: #00bfff">
          <span class="glyphicon glyphicon-star"></span>最新资讯
        </div>
        <div class="row">
          <div class="col-md-6" style="margin-bottom: 15px">
            <div id="carousel-box" class="carousel slide" data-ride="carousel">
              <!-- Indicators -->
              <ol class="carousel-indicators">
                <li
                  data-target="#carousel-box"
                  data-slide-to="0"
                  class="active"
                ></li>
                <li data-target="#carousel-box" data-slide-to="1"></li>
                <li data-target="#carousel-box" data-slide-to="2"></li>
              </ol>

              <!-- Wrapper for slides -->
              <div id="listbox" class="carousel-inner" role="listbox">
             
              </div>

              <!-- Controls -->
              <a
                class="left carousel-control"
                href="#carousel-box"
                role="button"
                data-slide="prev"
              >
                <span
                  class="glyphicon glyphicon-chevron-left"
                  aria-hidden="true"
                ></span>
                <span class="sr-only">Previous</span>
              </a>
              <a
                class="right carousel-control"
                href="#carousel-box"
                role="button"
                data-slide="next"
              >
                <span
                  class="glyphicon glyphicon-chevron-right"
                  aria-hidden="true"
                ></span>
                <span class="sr-only">Next</span>
              </a>
            </div>
          </div>
          <div class="col-md-6" style="margin-bottom: 15px">
            <div id="news-box">
              <ul id="news">
                
              </ul>
            </div>
          </div>
        </div>
        <div class="page-header" style="margin-top: 0; color: #00bfff">
          <span class="glyphicon glyphicon-star"></span>水产品科普
        </div>
        <div id="box">
          <div class="father">
            <div class="son">
              <a href=""><img class="img-thumbnail" src="img/bg1.jpg" /></a>
            </div>
            <div class="son">
              <a href=""><img class="img-thumbnail" src="img/bg1.jpg" /></a>
            </div>
            <div class="son">
              <a href=""><img class="img-thumbnail" src="img/bg1.jpg" /></a>
            </div>
            <div class="son">
              <a href=""><img class="img-thumbnail" src="img/bg1.jpg" /></a>
            </div>
            <div class="son">
              <a href=""><img class="img-thumbnail" src="img/bg1.jpg" /></a>
            </div>
          </div>
        </div>
      </div>
      <div id="footer"></div>
    </div>
    <script>
      $(function () {
        $("#header").load("./header.html");
        $("#footer").load("./footer.html");
        var timer = setInterval(function () {
          $("#news li:last").prependTo("#news").hide().slideDown();
        }, 2000);
        $("#news").hover(
          function () {
            clearInterval(timer);
          },
          function () {
            timer = setInterval(function () {
              $("#news li:last").prependTo("#news").hide().slideDown();
            }, 2000);
          }
        );
        //图片循环函数
        function round() {
          $(".son").each(function (index, item) {
            $(this).clone().appendTo(".father");
          });

          $(".father").width($(".son").length * 200 + "px");
          var width = -(JSON.parse($(".father").width()) / 2);

          var left = 0;
          var timer = null;
          function scroll() {
            timer = setInterval(function () {
              left = left - 10;
              if (left <= width) {
                left = 0;
              }
              $(".father").css({ left: left });
            }, 200);
          }
          scroll();
          //鼠标移上去停止动画
          $("#box")
            .mouseenter(function () {
              clearInterval(timer);
            })
            .mouseleave(function () {
              scroll();
            });
        }
        //发送轮播图请求
        $.ajax({
          type: "get",
          url: "/news/more",
          success: (res) => {
            var html = "";
            res.data.forEach((item, index) => {
              html += `<div class="item ${index == 0 ? "active" : ""}">
                  <a style="display:block" href="info.html?id=${item._id}">
                  <img
                    style="height: 260px; width: 100%"
                    src="${item.img}"
                    alt="图片"
                  />
                </a>
                
                </div> `;
            });
            $("#listbox").html(html);
          },
          error: (xhr, status, error) => {
            console.log("出错了", error);
          },
        });
        //发送滚动新闻请求
        $.ajax({
          type: "get",
          url: "/news?page=1",
          success: (res) => {
            let html = "";
            res.data.forEach((item) => {
              html += `
            <li>
                  <div class="news-item-img"><img src="${item.img}" alt="图片" /></div>
                  <div class="news-item-box">
                    <div class="news-item-box-title"><a href="info.html?id=${item._id}">${item.title}</a></div>
                    <div class="news-item-content">${item.content}</div>
                  </div>
                </li>
            `;
            });
            $("#news").html(html);
          },
          error: (xhr, status, error) => {
            console.log("出错了", error);
          },
        });
        //发送循环图片请求
        $.ajax({
          type:'get',
          url:'/aquatic?page=1',
          success:res=>{
            let html = '';
            res.data.forEach(item=>{
               html +=`
               <div class="son">
              <a href="infocontent.html?id=${item._id}"><img class="img-thumbnail" src="${item.img}" alt="图片" /></a>
            </div>
               `
            })                                         
            $(".father").html(html);
            round();
          },
          error:(xhr, status, error)=>{
            console.log("出错了", error);
          }
        })
      });
    </script>
  </body>
</html>
